import { Provide } from '@midwayjs/core';
import { BaseService } from '@cool-midway/core';
import { InjectEntityModel } from '@midwayjs/typeorm';
import { In, Repository } from 'typeorm';
import { OrderShareEntity } from '../entity/share';
import { OrderOrderEntity } from '../entity/order';
import { OrderCityEntity } from '../entity/city';
import { OrderShopEntity } from '../entity/shop';
import { UserInfoEntity } from '../../user/entity/info';

/**
 * 订单模块-店铺信息Service
 */
@Provide()
export class OrderShareService extends BaseService {
    @InjectEntityModel(OrderShareEntity)
    orderShareEntity: Repository<OrderShareEntity>;

    @InjectEntityModel(OrderOrderEntity)
    orderOrderEntity: Repository<OrderOrderEntity>;

    @InjectEntityModel(OrderCityEntity)
    orderCityEntity: Repository<OrderCityEntity>;

    @InjectEntityModel(OrderShopEntity)
    orderShopEntity: Repository<OrderShopEntity>;

    @InjectEntityModel(UserInfoEntity)
    userInfoEntity: Repository<UserInfoEntity>;

    // 重写page方法
    async page(query, options) {
        console.log(query, options);
        const result = await super.page(query, options); // 先获取所有数据 
        // 获取所有的订单号
        const orderIds = result.list.map(item => item.sysOrderId);
        // 根据订单号查询订单信息
        const orders = await this.orderOrderEntity.find({
            where: {
                id: In(orderIds),
            },
        });
        // 提取城市id，店铺id，用户id
        const userIds = orders.map(order => order.userId);
        // 根据城市id，店铺id，用户id查询城市、店铺、用户信息
        const cities = await this.orderCityEntity.find();
        const shops = await this.orderShopEntity.find();
        const users = await this.userInfoEntity.find({
            where: {
                id: In(userIds),
            },
        });
        // 将所有订单信息添加到结果中
        result.list.forEach(item => {
            const order = orders.find(order => order.id === item.sysOrderId);
            // 拼接上城市、店铺、用户、支付金额
            const shop = shops.find(shop => shop.id === order?.shopId || 0);
            const city = cities.find(city => city.id === shop?.cityId || 0);
            const user = users.find(user => user.id === order?.userId || 0);
            // item.order = order;
            item.shopName = shop?.name || '';
            item.cityName = city?.cityName || '';
            item.userName = user?.nickName || '';
            item.userPhone = user?.phone || '';
            item.orderAmount = order?.payAmount || 0;
            // 对电话号脱敏
            if (item.userPhone != '') {
                item.userPhone = item.userPhone.replace(/(\d{3})\d{4}(\d{4})/, '$1****$2');
            }
        });
        return result;
    }

    // 写page2方法
    async caiwuPage(query, options) {
        console.log(query, options);
        const result = await super.page(query, options); // 先获取所有数据 
        // 获取所有的订单号
        // const orderIds = result.list.map(item => item.orderId);
        // // 根据订单号查询订单信息
        // const orders = await this.orderOrderEntity.find({
        //     where: {
        //         id: In(orderIds),
        //     },
        // });
        // // 将所有订单信息添加到结果中
        // result.list.forEach(item => {
        //     const order = orders.find(order => order.id === item.orderId);
        //     item.order = order;
        // });
        return result;
    }
}
